package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import com.tedu.weibo.pojo.vo.WeiboIndexVO;
import com.tedu.weibo.result.JsonResult;
import com.tedu.weibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.sql.rowset.JdbcRowSet;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
@Api(tags = "微博模块")
@Validated
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/insert")
    @ApiOperation("发表微博")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO,@ApiIgnore HttpSession session){
        System.out.println("开始发表微博...");
        System.out.println(weiboDTO);
        //验证登录
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        if (user==null){//没登录
            return new JsonResult(Status.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        weibo.setContent(weiboDTO.getContent());
        weibo.setCreated(new Date());
        weibo.setUserId(user.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.success();
    }

    @GetMapping("/selectIndex")
    @ApiOperation("查询首页微博列表")
    public JsonResult findIndexWeibo(){
        /*
            1:首先判断redis中是否缓存了首页上的微博数据
            2:如果有则直接将数据返回给浏览器
            3:如果没有
                3.1:先从数据库中将数据查询出来
                3.2:将数据存入到redis中
                3.3:将数据返回给浏览器
         */
        ValueOperations ops = redisTemplate.opsForValue();
        List<WeiboIndexVO> list = (List<WeiboIndexVO>)ops.get("weiboList");
        if(list==null){
            list = weiboMapper.selectAll();
            ops.set("weiboList",list);
        }
        //List<WeiboIndexVO> list = weiboMapper.selectAll();
        return JsonResult.success(list);
    }

    @GetMapping("/selectById")
    @ApiOperation("查询指定微博")
    /*
    * 如果是在方法上直接接收参数,而不是DTO形式,可以使用@ApiImplicitParam注解说明参数,
    * 其中:
    * value:该参数的描述
    * name:制定方法参数的名字(为哪个参数添加说明)
    * required:该参数是否必须传递
    * example:该参数的示例值
    * dataType:参数类型
    * */
    @ApiImplicitParam(value = "微博id",name = "id",required = true,example = "10",dataType = "int")
    public JsonResult findById(@Range(min = 1,max = 10,message = "微博只能在1-10之间") @RequestParam Integer id){
        if (id==null||id<=0){
            throw new IllegalArgumentException("微博id不合法");
        }
        return JsonResult.success(weiboMapper.selectById(id));
    }
}
